// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "TookUpItemBase.h"
#include "CPlayerCharacter.h"


ATookUpItemBase::ATookUpItemBase()
{
	_displayUISphere = CreateDefaultSubobject<USphereComponent>(TEXT("UIBlocker"));
	_tookupBox = CreateDefaultSubobject<UBoxComponent>(TEXT("TookUpBox"));
	_tookupBox->SetBoxExtent(FVector(10, 10, 10));
	_tookupBox->SetCollisionProfileName(TEXT("TookUpItem"));
	//_tookupBox->SetCollisionProfileName(TEXT("BuildingTrace"));
	//_tookupBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	_tookupBox->SetSimulatePhysics(true);
	RootComponent = _tookupBox;

	_displayMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DisplayMesh"));
	_displayMesh->AttachTo(_tookupBox);
	_displayMesh->SetCollisionProfileName(TEXT("TookUpItem"));

	_skDisplayMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkDisplayMesh"));
	_skDisplayMesh->AttachTo(_tookupBox);
	_skDisplayMesh->SetCollisionProfileName(TEXT("TookUpItem"));

	_displayUISphere->AttachTo(_tookupBox);
	_displayUISphere->SetSphereRadius(400);
	_displayUISphere->SetCollisionProfileName(TEXT("OverlapPlayer"));
	_displayUISphere->OnComponentBeginOverlap.AddDynamic(this, &ATookUpItemBase::OnItemBeginOverlap);
	_displayUISphere->OnComponentEndOverlap.AddDynamic(this, &ATookUpItemBase::OnItemEndOverlap);

	_canAsTookUpItem = true;
}

void ATookUpItemBase::TookUp()
{
	AUIManager::_UIManager->RemoveDisplayItemIcon(this);
	Super::TookUp();
}

void ATookUpItemBase::BeginPlay()
{
	ConstructItem(CreateType, TookUpAmount);
}

void ATookUpItemBase::ConstructItem(int32 type, int32 count)
{
	_tookUpAmount = count;
	_itemType = type;
	FItemDisplay* showOutInfo = ItemType::GetItemDisplayByID(_itemType);
	_displayTexture = showOutInfo->_displayIcon;
	_skDisplayMesh->SetSkeletalMesh(nullptr);
	_displayMesh->SetStaticMesh(nullptr);
	if (showOutInfo->_skMeshData != nullptr)
	{
		_skDisplayMesh->SetSkeletalMesh(showOutInfo->_skMeshData);
	}
	if (showOutInfo->_stMeshData != nullptr)
	{
		_displayMesh->SetStaticMesh(showOutInfo->_stMeshData);
	}
}

int ATookUpItemBase::GetAdditionValue()
{
	return AdditionValue;
}

void ATookUpItemBase::OutlineOn()
{
	_skDisplayMesh->bRenderCustomDepth = true;
	_displayMesh->bRenderCustomDepth = true;
	_skDisplayMesh->SetCustomDepthStencilValue(255);
	_displayMesh->SetCustomDepthStencilValue(255);
}

void ATookUpItemBase::OutlineOff()
{
	_skDisplayMesh->bRenderCustomDepth = false;
	_displayMesh->bRenderCustomDepth = false;
	_skDisplayMesh->SetCustomDepthStencilValue(0);
	_displayMesh->SetCustomDepthStencilValue(0);
}

void ATookUpItemBase::PerceptionOutlineOn()
{
	_skDisplayMesh->bRenderCustomDepth = true;
	_displayMesh->bRenderCustomDepth = true;
	_skDisplayMesh->SetCustomDepthStencilValue(252);
	_displayMesh->SetCustomDepthStencilValue(252);
}

void ATookUpItemBase::PerceptionOutlineOff()
{
	_skDisplayMesh->bRenderCustomDepth = false;
	_displayMesh->bRenderCustomDepth = false;
	_skDisplayMesh->SetCustomDepthStencilValue(0);
	_displayMesh->SetCustomDepthStencilValue(0);
}

UTexture2D* ATookUpItemBase::GetDisplayItemIcon()
{
	return _displayTexture;
}

FVector ATookUpItemBase::GetDisplayItemWorldPosition()
{
	return GetActorLocation();
}

void ATookUpItemBase::OnItemBeginOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (Cast<ACPlayerCharacter>(OtherActor)!=nullptr)
	{
		AUIManager::_UIManager->AddDisplayItemIcon(this);
	}
}

void ATookUpItemBase::OnItemEndOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (Cast<ACPlayerCharacter>(OtherActor) != nullptr)
	{
		AUIManager::_UIManager->RemoveDisplayItemIcon(this);
	}
}
